package com.github.postsanf.yinian.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.CouponListAdapter;
import com.github.postsanf.yinian.bean.YNCouPon;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNCouponResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class CouponListActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView couponlist_list;
    private CouponListAdapter mAdapter;
    private List<YNCouPon> ynCouPonList = new ArrayList<>();
    private EditText couponlist_code;
    private RelativeLayout rl_skip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_couponlist);
        initView();
        initCouponList();
    }

    private void initView() {
        findViewById(R.id.couponlist_back).setOnClickListener(this);
        findViewById(R.id.couponlist_exchange).setOnClickListener(this);
        couponlist_code = (EditText) findViewById(R.id.couponlist_code);
        couponlist_list = (RecyclerView) findViewById(R.id.couponlist_list);
        couponlist_list.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CouponListAdapter(CouponListActivity.this, ynCouPonList);
        couponlist_list.setAdapter(mAdapter);
        couponlist_list.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                closeKeyword();
                return false;
            }
        });

        rl_skip = (RelativeLayout) findViewById(R.id.rl_skip);
    }

    private void closeKeyword() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(couponlist_code.getWindowToken(), 0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.couponlist_back:
                finish();
                break;
            case R.id.couponlist_exchange:
                String code = couponlist_code.getText().toString();
                if (StringUtils.isEmpty(code)) {
                    showToast("请填写优惠码");
                    return;
                }
                exchangeCouponCode(code);
                break;
        }
    }

    private void initCouponList() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynShowCouponList))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCouponResponse resCoupon = gson.fromJson(s, YNCouponResponse.class);
                        if (ReqUtils.isSuccess(resCoupon.getCode())) {
                            ynCouPonList.clear();
                            ynCouPonList.addAll(resCoupon.getData());
                            mAdapter.notifyDataSetChanged();
                            if (resCoupon.getData().size() != 0) {
                                rl_skip.setVisibility(View.GONE);
                            } else {
                                rl_skip.setVisibility(View.VISIBLE);
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }


    private void exchangeCouponCode(String couponCode) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        mParam.put("couponCode", couponCode);
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynGetCouponByCouponCode))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCouponResponse resCoupon = gson.fromJson(s, YNCouponResponse.class);
                        if (ReqUtils.isSuccess(resCoupon.getCode())) {
//                            ynCouPonList.clear();
//                            ynCouPonList.addAll(resCoupon.getData());
//                            mAdapter.notifyDataSetChanged();
                            initCouponList();
                            showToast("兑换成功");
                            closeKeyword();
                            couponlist_code.setText("");
                        } else {
                            showToast(resCoupon.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

}
